<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 最长的美好子字符串 -->
    <script>
      var longestNiceSubstring = function (s) {
        let ans = ''
        for (let i = 0; i < s.length; i++) {
          // 大写字母二进制状态
          let upper = 0
          // 小写字母二进制维护状态
          let lower = 0
          for (let j = i; j < s.length; j++) {
            const pos = s[j].toLocaleLowerCase().charCodeAt() - 97
            if (/[a-z]/.test(s[j])) {
              lower |= 1 << pos
            } else {
              upper |= 1 << pos
            }
            // 如果 lower ^ upper 是 0，那代表当前子串符合条件
            if ((lower ^ upper) === 0 && ans.length < j - i + 1) {
              ans = s.substring(i, j + 1)
            }
          }
        }
        return ans
      }
      console.log(longestNiceSubstring('YazaAay'))
    </script>
  </body>
</html>
